const app = getApp()
const util = require('../util/util.js')

Page({

  data: {
    gameHistory: [],
    taskList: [],
    gold: 0
  },

  onLoad: function(options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#58B5F0',
    })
    this.setData({
      gold: app.globalData.gold,
      taskList: app.globalData.taskList,
    })
  },

  onShow: function() {
    this.getGameHistory()
    this.setData({
      gold: app.globalData.gold,
      taskList: app.globalData.taskList,
    })
  },

  // 从storage中取出最近玩过的游戏列表
  getGameHistory: function() {
    var list = wx.getStorageSync('baibao_game_history')
    if (list) {
      list = this.beSet(list)
      wx.setStorageSync('baibao_game_history', list)
      this.setData({
        gameHistory: list.reverse()
      })
    }
    this.handle_game_num()
  },

  // 根据id去重
  beSet: function(arr) {
    var result = [],
      i, j, len = arr.length;
    for (i = 0; i < len; i++) {
      for (j = i + 1; j < len; j++) {
        if (arr[i].appid === arr[j].appid) {
          j = ++i;
        }
      }
      result.push(arr[i]);
    }
    return result;
  },

  // 根据玩过的游戏数量,处理每日任务
  handle_game_num: function() {
    var length = this.data.gameHistory.length
    var taskList = util.getTaskList()
    if (length >= 1 && length <5) {
      if (!taskList.one_game) {
        taskList.one_game = 1
      }
    }
    if (length >= 5) {
      if (!taskList.one_game) {
        taskList.one_game = 1
      }
      if (!taskList.five_game) {
        taskList.five_game = 1
      }
    }
    util.setTaskList(taskList)
  },

  // 跳转小程序(判断是否有二维码图片,有则展示)
  goMiniProgram: function(e) {
    var game = e.currentTarget.dataset.game
    var list = wx.getStorageSync('baibao_game_history')
    list.push(game)
    wx.setStorageSync('baibao_game_history', list)
    if (game.code_url) {
      wx.previewImage({
        current: game.code_url,
        urls: [game.code_url]
      });
    } else {
      wx.navigateToMiniProgram({
        appId: game.appid,
        path: '',
        success(res) {
          // 添加最近玩过
        }
      });
    }
  },

  onShareAppMessage: function () {
    var taskList = util.getTaskList()
    if (!taskList.share) {
      taskList.share = 1
      util.setTaskList(taskList)
    }
  },

  // 抽奖
  choujiang: function () {
    wx.navigateTo({
      url: "/pages/mine/choujiang/choujiang"
    });
  },

  // 签到
  sign: function() {
    var taskList = util.getTaskList()    
    if (!taskList.sign) {
      // 处理金币
      app.globalData.gold += 100
      wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
      taskList.sign = 1
      util.setTaskList(taskList)
      this.onShow()
    }
  },

  // 领取分享奖励
  getShareGold: function() {
    var taskList = util.getTaskList()
    if (taskList.share == 1) {
      // 处理金币
      app.globalData.gold += 200
      wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
      taskList.share = 2
      util.setTaskList(taskList)
      this.onShow()
    }
  },

  // 领取一次游戏奖励
  getOneGameGold: function () {
    var taskList = util.getTaskList()
    if (taskList.one_game == 1) {
      // 处理金币
      app.globalData.gold += 300
      wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
      taskList.one_game = 2
      util.setTaskList(taskList)
      this.onShow()
    }
  },

  // 领取五次游戏奖励
  getFiveGameGold: function () {
    var taskList = util.getTaskList()
    if (taskList.five_game == 1) {
      // 处理金币
      app.globalData.gold += 1000
      wx.setStorageSync('baibao_userinfo_gold', app.globalData.gold)
      taskList.five_game = 2
      util.setTaskList(taskList)
      this.onShow()
    }
  },
})